namespace Umbraco.Ecommerce.DataControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for test.
	/// </summary>
	public class UmbracoProductControl : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.CheckBox useMultipleProductVariants;
		protected System.Web.UI.WebControls.TextBox singleProductVariantProductId;
		protected System.Web.UI.WebControls.TextBox singleProductVariantPrice;
		protected System.Web.UI.HtmlControls.HtmlGenericControl dgProductVariantsHolder;
		protected System.Web.UI.HtmlControls.HtmlGenericControl singleProductInfoHolder;
		protected System.Web.UI.WebControls.DataGrid dgProductVariants;
		private umbraco.cms.businesslogic.CMSNode n;
		protected System.Web.UI.WebControls.Literal lttShowPrice;
		protected System.Web.UI.WebControls.TextBox newProductVariantName;
		protected System.Web.UI.WebControls.TextBox newProductId;
		protected System.Web.UI.WebControls.TextBox newProductVariantPrice;
		protected System.Web.UI.WebControls.Button btnCreateProductVariant;
		protected System.Web.UI.WebControls.Label errLabel;
		protected System.Web.UI.HtmlControls.HtmlGenericControl DIV1;
		protected System.Web.UI.WebControls.TextBox productName;
	
		private BusinessLogic.Product p;

		public umbraco.cms.businesslogic.CMSNode Node {
			get {return n;}
			set {
				n = value;
				p = new BusinessLogic.Product(n.Id);
			}
		}

		public void SaveInfo() {
			if (p.UseMultipleProductVariants) 
			{
				// Loop through all the items in the ProductVariant datagrid updating the info
				foreach (DataGridItem dgi in dgProductVariants.Items) 
				{
						((Label)dgi.FindControl("errLbl")).Text = "";
						int id = int.Parse(dgi.Cells[0].Text);
						string ProductVariantName = ((TextBox) dgi.FindControl("ProductVariantName")).Text;
						string productId = ((TextBox) dgi.FindControl("productId")).Text;
						decimal price = 0;
						try 
						{
							 price = decimal.Parse(((TextBox) dgi.FindControl("price")).Text,BusinessLogic.Store.InputNumberFormatInfo);
						}
						catch 
						{
						((Label)dgi.FindControl("errLbl")).Text = "<br />Error setting price on ProductVariant!";
						}
						foreach (BusinessLogic.ProductVariant v in p.ProductVariants)
							{
								if (v.Id == id) 
								{
									v.Price = price;
									v.ProductId = productId;
									v.Name = ProductVariantName;
								}
						}
					} 
				// PopulateDataGrid();
			} 
			else {
				try 
				{
					p.ProductVariants[0].Price = decimal.Parse(singleProductVariantPrice.Text, BusinessLogic.Store.InputNumberFormatInfo);
				}
				catch{
				}
				p.ProductVariants[0].ProductId = singleProductVariantProductId.Text;
			}
				p.Name = productName.Text;
			}

		private void Page_Load(object sender, System.EventArgs e)
		{
			

			
			if (!IsPostBack)
				productName.Text = n.Text;

				if (p.UseMultipleProductVariants) 
				{
					dgProductVariantsHolder.Visible = true;
					singleProductInfoHolder.Visible=false;
					PopulateDataGrid();
				}
				else
				{
					dgProductVariantsHolder.Visible = false;
					singleProductInfoHolder.Visible= true;
					singleProductVariantProductId.Text = p.ProductVariants[0].ProductId;
					singleProductVariantPrice.Text = p.ProductVariants[0].Price.ToString();
				}
			useMultipleProductVariants.Checked = p.UseMultipleProductVariants;
		}

		private void PopulateDataGrid() {
			DataTable dt = new DataTable();

			dt.Columns.Add("ProductId");
			dt.Columns.Add("ProductVariantName");
			dt.Columns.Add("ProductVariantShowPrice");
			dt.Columns.Add("Price");
			dt.Columns.Add("Id");

			foreach (BusinessLogic.ProductVariant v in p.ProductVariants) 
			{
				DataRow dr = dt.NewRow();
				dr["ProductId"] = v.ProductId;
				dr["ProductVariantName"] = v.Name;
				dr["Price"] = v.Price;
				dr["Id"] = v.Id;
				dr["ProductVariantShowPrice"] = v.Price.ToString("c",BusinessLogic.Store.NumberFormatInfo);
				dt.Rows.Add(dr);
			}

			dgProductVariants.DataSource = dt;
			dgProductVariants.DataBind();
		}

		private void useMultipleProductVariants_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.useMultipleProductVariants.Checked) 
			{
				p.UseMultipleProductVariants = true;
				dgProductVariantsHolder.Visible = true;
				singleProductInfoHolder.Visible=false;
				PopulateDataGrid();
			}
			else
			{
				if (p.ProductVariants.Count > 1) 
				{
					this.Page.RegisterClientScriptBlock("Alert","<script>alert('You need to remove all but 1 ProductVariant in order to use single ProductVariant product');</script>");
				} 
				else 
				{
					p.UseMultipleProductVariants = false;
					dgProductVariantsHolder.Visible = false;
					singleProductInfoHolder.Visible= true;
					singleProductVariantProductId.Text = p.ProductVariants[0].ProductId;
					singleProductVariantPrice.Text = p.ProductVariants[0].Price.ToString();
				}
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.useMultipleProductVariants.CheckedChanged += new System.EventHandler(this.useMultipleProductVariants_CheckedChanged);
			this.btnCreateProductVariant.Click += new System.EventHandler(this.btnCreateProductVariant_Click);
			this.dgProductVariants.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgProductVariants_ItemCommand);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		
		protected void dgProductVariants_ItemCommand(object sender, DataGridCommandEventArgs e) 
		{
			// Delete propertytype from contenttype
			if (e.CommandName == "Delete") 
			{
				if (p.ProductVariants.Count > 1) 
				{
					int Id = int.Parse(e.Item.Cells[0].Text);
					p.RemoveProductVariant(Id);
					PopulateDataGrid();
				}
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender (e);
			lttShowPrice.Text = p.ProductVariants[0].Price.ToString("c",BusinessLogic.Store.NumberFormatInfo);
		}

		private void btnCreateProductVariant_Click(object sender, System.EventArgs e)
		{
			// add new ProductVariant!!
			if (newProductId.Text != "" && newProductVariantName.Text != "" && newProductVariantPrice.Text != "") 
			{
				try 
				{
					if (!p.AddProductVariant(newProductId.Text,newProductVariantName.Text,decimal.Parse(newProductVariantPrice.Text,BusinessLogic.Store.InputNumberFormatInfo)))
					{
						errLabel.Text = "Couldnt create ProductVariant due to invalid productId";
						} 
					else 
					{
						newProductId.Text = "";
						newProductVariantName.Text = "";
						newProductVariantPrice.Text = "";
						errLabel.Text= "";
						PopulateDataGrid();
					}

				}
				catch {
					errLabel.Text = "Couldnt create ProductVariant due to invalid price";
				}
						
			} 
			else {
				errLabel.Text = "You need to fill out all fields in order to create a new ProductVariant";
			}

		}

	
	
	}
}
